﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace ZD_Utility
{
    public static class ZD_PageExtension
    {
        #region 检查指定键名的QueryString是否能被转换成数字
        /// <summary>
        /// 检查指定键名的QueryString是否能被转换成数字
        /// </summary>
        /// <param name="key">QueryString键名</param>
        /// <returns></returns>
        public static bool IsQueryStringInt(this Page page, string key)
        {
            return page.Request.QueryString[key].IsNum();
        }
        #endregion

        #region 将指定键名的QueryString转换成数字并返回
        /// <summary>
        /// 将指定键名的QueryString转换成数字并返回
        /// </summary>
        /// <param name="key">QueryString键名</param>
        /// <returns></returns>
        public static int GetQueryStringAsInt(this Page page, string key)
        {
            return page.Request.QueryString[key].ToInt();
        }
        #endregion

        #region 检查DataSet是否为空
        public static bool Check(this DataSet ds)
        {
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 绑定Repeater，绑定前检查数据源是否为空
        public static void BindWithCheck(this System.Web.UI.WebControls.Repeater rp, object data)
        {
            BindWithCheck(rp, data, true);
        }

        public static void BindWithCheck(this System.Web.UI.WebControls.Repeater rp, object data, bool NoDataTip)
        {
            if ((data is DataSet && ((DataSet)data).Tables.Count <= 0) || data.IsNull())
            {
                if (NoDataTip)
                {
                    Literal lit = new Literal();
                    lit.Text = "<font style=\"color:red;\">暂无数据！</font>";
                    rp.Controls.Add(lit);
                }
            }
            else
            {
                rp.DataSource = data;
                rp.DataBind();
            }
        }
        #endregion
    }
}
